public class HashTableImpl implements HashTable {
	int myArraySize = 3;
SingleLinkedListImpl[] table = new SingleLinkedListImpl[myArraySize];
int maxSizeOfList = 5;

	public boolean isEmpty() {
	boolean answer = true;
//	for (int i=0; i<table.length; i++) {
//		if (table[i] != null) {
//			answer = false;
//			}
	if (size()>0) {
		answer = false;
		}
	return answer;
	}
	
	public int size() {
	int answer = 0;
	int i=0;
	while (i<table.length) {
		if (table[i] != null) {
			answer = answer + table[i].size();	
			}
		i++;
		}
	return answer/2;
	}
	
	public void put(String key, String value) {
		put(key, value, table);
	}
	
	private void put(String key, String value, SingleLinkedListImpl[] tempTable) {
	int t = key.hashCode()%table.length;
	if (tempTable[t] != null) {
		boolean existing = false;
		int n = tempTable[t].size();
		for (int i = 0; i<n; i=i+2){
			if (key.equals(tempTable[t].get(i))) {
				i = i+1;
				tempTable[t].remove(i);
				tempTable[t].insert(value, i);
				existing = true;
				break;
				}
			}
		if (! existing) {
			tempTable[t].insertLast(key);
			tempTable[t].insertLast(value);
			}
		}
	else {
		tempTable[t] = new SingleLinkedListImpl();
		tempTable[t].setMaxSize(maxSizeOfList);
		tempTable[t].insertLast(key);
		tempTable[t].insertLast(value);
		}
	if (tempTable[t].size()>maxSizeOfList) {
		rehash();
	}
	}
	
	
	
	public String get(String key){
	int t = key.hashCode()%table.length;
	String answer = null;
	if (table[t] != null) {
		int n = table[t].size();
		for (int i = 0; i<n; i=i+2){
			if (key.equals(table[t].get(i))) {
				i++;
				answer = table[t].get(i);
				}
			}
		}
	return answer;
	}
	
	public String remove(String key){
		System.err.println("removing key="+key);
		int t = key.hashCode()%table.length;
		String answer = null;
		if (table[t] != null) {
			int n = table[t].size();
			for (int i = 0; i<n; i=i+2){
				System.err.println("i="+i+" key="+table[t].get(i));
				if (key.equals(table[t].get(i))) {
					System.err.println("now removing ="+table[t].get(i));
					table[t].remove(i);
					answer = table[t].get(i);
					System.err.println("now removing ="+table[t].get(i));
					table[t].remove(i);
					i=i-2;
					n=n-2;
					System.err.println(isEmpty());
					}
				}
			}	
		return answer;
	}
	
	public void clear(){
		for (int i = 0; i < table.length; i++){
			table[i]=null;
		}
	}
	
	private void rehash(){
		int n=table.length;
		SingleLinkedListImpl[] tempTable = new SingleLinkedListImpl[2*n];
		for (int i=0; i<n; i++){
			for (int j=0; j<table[i].size(); j=j+2){
				put(table[i].get(j), table[i].get(j+1), tempTable);
			}
		}
		table=tempTable;
	}
	
}